package com.wchat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wchat.bean.entity.Friends;
import com.wchat.bean.entity.Users;
import com.wchat.bean.vo.FriendsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : willian fu
 * @date : 2021/8/27
 */
public interface FriendsMapper extends BaseMapper<Friends> {

    @Select("SELECT friend_id FROM friends WHERE user_id = #{userId} AND status=1 UNION ALL " +
            "SELECT user_id FROM friends WHERE friend_id = #{userId} AND status=1")
    List<Integer> getUserFriends(@Param("userId") Integer userId);

    @Select("SELECT u.nick, u.user_name, u.avatar, u.sex, fs.* FROM " +
            "(SELECT f.user_id, f.u_remark remark, f.created FROM friends f WHERE f.friend_id = #{userId}  AND f.status = #{status} UNION ALL " +
            "SELECT f2.friend_id user_id, f2.u_remark remark, f2.created FROM friends f2 WHERE f2.user_id = #{userId}  AND f2.status = #{status}) fs" +
            ", users u WHERE u.user_id = fs.user_id")
    List<FriendsVo> getFriends(@Param("userId") Integer userId, @Param("status") Integer status);

    @Select("SELECT f.id, u.user_id, u.nick, u.user_name, u.avatar, u.sex FROM users u, friends f WHERE u.user_id = f.user_id AND f.status = 0 AND f.friend_id = #{userId}")
    List<FriendsVo> getNewFriends(@Param("userId") Integer userId);
}
